<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="课题组名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="课题组名称"></el-input>
      </el-form-item>
      <el-form-item prop="creatorId" label="用户">
        <!--        <el-select v-model="dataForm.creatorId" placeholder="选择用户" @change="changeUser">
          <el-option v-for="user in userList" :key="user.id" :label="user.username"
                     :value="user.id"></el-option>
        </el-select>-->
        <el-select v-model="dataForm.creatorId" filterable remote reserve-keyword placeholder="输入手机号或姓名"
                   :remote-method="getUserList" :loading="loading" @change="changeUser">
          <el-option v-for="item in userList" :key="item.value" :label="item.username" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="创建人手机号" prop="phone">
              <el-input v-model="dataForm.phone" placeholder="创建人手机号"></el-input>
            </el-form-item>-->

      <!--      <el-form-item label="是否认证，0否，1是" prop="authentication">
              <el-input v-model="dataForm.authentication" placeholder="是否认证，0否，1是"></el-input>
            </el-form-item>-->
      <el-form-item label="余额" prop="balance">
        <el-input v-model="dataForm.balance" max="99999999.99" placeholder="余额"
                  @input="dataForm.debtLimit = Math.min(dataForm.debtLimit, 99999999.99)"></el-input>
      </el-form-item>
      <el-form-item label="欠款限额" prop="debtLimit">
        <el-input type="number" max="99999999.99" v-model="dataForm.debtLimit" placeholder="欠款限额"
                  @input="dataForm.debtLimit = Math.min(dataForm.debtLimit, 99999999.99)"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder="备注" type="textarea" rows="2"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
            </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const userList = ref<any[]>([]);
const loading = ref(false);

const dataForm = reactive({
  id: "",
  name: "",
  creatorId: "",
  creator: "",
  phone: "",
  note: "",
  status: 1,
  balance: 0,
  debtLimit: 0
});

const rules = ref({
  name: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  creatorId: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // phone: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  // note: [
  //   {required: true, message: '必填项不能为空', trigger: 'blur'}
  // ],
  balance: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  debtLimit: [{required: true, message: "必填项不能为空", trigger: "blur"}]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  Promise.all([getUserList("")]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/team/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取用户列表
const getUserList = async (query: string) => {
  loading.value = true;
  const res = await baseService.get("/yckj/user/list", {key: query});
  userList.value = res.data;
  loading.value = false;
};

const changeUser = (value: number) => {
  const selectedItem = userList.value.find((item) => item.id === value);
  dataForm.creator = selectedItem.username;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/team", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
